import 'package:dio/dio.dart';
import 'package:flutter_getx/app/util/dio/app_response.dart';
import 'package:flutter_getx/app/util/dio/app_dio.dart';
import 'package:get/get.dart';

class DioService extends GetxService {
  late Dio _dio;

  Future<DioService> init() async {
    _dio = AppDio.getDioInstance();
    return this;
  }

  Future<AppResponse> get(
    String path, {
    Map<String, dynamic>? queryParameters,
    Options? options,
    CancelToken? cancelToken,
    ProgressCallback? onReceiveProgress,
  }) async {
    try {
      var response = await _dio.get(path,
          queryParameters: queryParameters,
          options: options,
          cancelToken: cancelToken,
          onReceiveProgress: onReceiveProgress);
      return response.data;
    } catch (e){
      Get.defaultDialog(title: e.toString());
      return AppResponse.failure(e.toString());
    }
  }

  Future<AppResponse> post(
    String path, {
    data,
    Map<String, dynamic>? queryParameters,
    Options? options,
    CancelToken? cancelToken,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    try {
      var response = await _dio.post(
        path,
        data: data,
        queryParameters: queryParameters,
        options: options,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        onReceiveProgress: onReceiveProgress,
      );
      return response.data;
    } catch(e) {
      Get.defaultDialog(title: e.toString());
      return AppResponse.failure(e.toString());
    }
  }
}
